import re
from fastapi import HTTPException, Query
from pydantic import BaseModel, field_validator

from forward.common.enums import LoginTypeEnum, LoginClientEnum
from forward.exceptions.base import AppException
from forward.http_base import HttpResp
from forward.utils.tools import ToolsUtil


class FrontLoginCheckOutSchema(BaseModel):
    """
    登录管理 返回信息
    """

    user_id: int
    token: str
    is_band_mobile: bool
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class FrontRegisterInSchema(BaseModel):
    """
    注册 入参
    """

    username: str
    password: str = Query(min_length=6, max_length=12)
    client: LoginClientEnum

    @field_validator("username", mode="before")
    def check_username(cls, value):
        if not re.fullmatch(r"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{3,12}$", value):
            raise AppException(HttpResp.FAILED, msg="用户名格式不正确")
        return value


class FrontLoginCheckInSchema(BaseModel):
    """
    手机端-登录管理 入参
    """

    scene: LoginTypeEnum  # 登录方式
    client: LoginClientEnum  # 登录端
    username: str = Query(default=None)
    mobile: str = Query(default=None)
    password: str = Query(default=None)
    code: str = Query(default=None)

class AccountLoginInSchema(BaseModel):
    """
    手机端-登录管理 账号
    """

    scene: LoginTypeEnum  # 登录方式
    client: LoginClientEnum  # 登录端
    username: str = Query(default=None)
    password: str = Query(default=None)
    mobile: str = Query(default=None)
